package com.javalong.affairs.module.main.activity;

import java.util.HashMap;
import java.util.Map;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.javalong.affairs.R;
import com.javalong.affairs.base.activity.UIHandlerActivity;
import com.javalong.affairs.base.constant.DataType;
import com.javalong.affairs.base.constant.RequestKey;
import com.javalong.affairs.base.data.AllData;
import com.javalong.affairs.base.listener.BaseResponseListener;
import com.javalong.affairs.base.manager.IntentManager;
import com.javalong.affairs.base.model.User;
import com.javalong.affairs.base.utils.AppDataUtils;
import com.javalong.affairs.base.utils.HttpUtils;
import com.javalong.affairs.base.utils.StringUtils;
import com.javalong.affairs.base.utils.ToastUtils;
import com.javalong.affairs.base.utils.UrlUtils;
import com.javalong.affairs.module.main.ui.JLCheckBox;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.tencent.android.tpush.XGIOperateCallback;
import com.tencent.android.tpush.XGPushManager;

@ContentView(R.layout.activity_login)
public class LoginActivity extends UIHandlerActivity {
    @ViewInject(R.id.loginButton)
    private Button bt_login;
    @ViewInject(R.id.et_username)
    private EditText et_username;
    @ViewInject(R.id.et_pwd)
    private EditText et_pwd;
    @ViewInject(R.id.cb_auto)
    private JLCheckBox cb_autologin;
    @ViewInject(R.id.cb_remember)
    private JLCheckBox cb_remember;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ViewUtils.inject(this);
        bt_login.setOnClickListener(this);
        initData();
    }

    private void initData() {
        String userName = AppDataUtils.getString(this,
                getString(R.string.share_username));
        String password = AppDataUtils.getString(this,
                getString(R.string.share_password));
        if (!StringUtils.isEmpty(userName)) {
            et_username.setText(userName);
        }
        if (!StringUtils.isEmpty(password)) {
            et_pwd.setText(password);
        }
        boolean autoLogin = AppDataUtils.getBoolean(LoginActivity.this, getString(R.string.share_autologin));
        boolean remember = AppDataUtils.getBoolean(LoginActivity.this, getString(R.string.share_remember));
        cb_autologin.initWithData(autoLogin);
        cb_remember.initWithData(remember);
        if (autoLogin) {
            bt_login.performClick();
        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.loginButton:
                if (checkEdit()) {
                    String url = UrlUtils.getUrl("User", "login");
                    Map<String, Object> params = new HashMap<String, Object>();
                    params.put(RequestKey.USERNAME, et_username.getText()
                            .toString());
                    params.put(RequestKey.PASSWORD, et_pwd.getText().toString());
                    params.put(RequestKey.LASTTYPE, 3);
                    progressDialog.show();
                    HttpUtils.post(url, params, new BaseResponseListener(
                            DataType.USER) {
                        @Override
                        public void onSuccess(Object obj) {
                            super.onSuccess(obj);
                            User user = (User) obj;
                            AllData.GetInstance().registerData(
                                    R.string.data_user_current_user, obj);
                            IntentManager.doActivity(LoginActivity.this,
                                    GuideActivity.class);
                            XGPushManager.registerPush(LoginActivity.this,
                                    user.userName, new XGIOperateCallback() {
                                        @Override
                                        public void onSuccess(Object arg0, int arg1) {
                                        }

                                        @Override
                                        public void onFail(Object arg0, int arg1,
                                                           String arg2) {
                                            Log.v("test", arg2);
                                        }
                                    });
                            progressDialog.dismiss();
                            /**
                             * 成功后保存 cb
                             */
                            AppDataUtils.saveBoolean(LoginActivity.this, getString(R.string.share_autologin), cb_autologin.isChecked());
                            AppDataUtils.saveBoolean(LoginActivity.this, getString(R.string.share_remember), cb_remember.isChecked());
                            AppDataUtils.saveString(LoginActivity.this,
                                    getString(R.string.share_username),
                                    user.userName);
                            if (cb_remember.isChecked()) {
                                String pwd = et_pwd.getText().toString();
                                AppDataUtils.saveString(LoginActivity.this,
                                        getString(R.string.share_password),
                                        pwd);
                            } else {
                                AppDataUtils.remove(LoginActivity.this, getString(R.string.share_password));
                            }
                        }
                    });
                }
                break;
            default:
                break;
        }
    }

    private boolean checkEdit() {
        if (StringUtils.isEmpty(et_username.getText().toString())
                || StringUtils.isEmpty(et_pwd.getText().toString())) {
            ToastUtils.showMessage("请输入用户名和密码");
            return false;
        }
        return true;
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        oneMoreOut();
    }
}
